fix(har): guard HAR logging when a request matches no mock [RQ-2398] - #40
fix(har): guard HAR logging when a request matches no mock [RQ-2398]#40dinex-dev wants to merge 2 commits into
Conversation
The HAR middleware's `finish` listener read `res.locals.rq_metadata.mockId` unconditionally. For requests that don't match any mock, `rq_metadata` is undefined, so this threw a `TypeError` inside the `finish` event callback — outside Express's request-handling context, where it surfaces as an uncaught exception rather than a handled request error. - Read the id via optional chaining (`res.locals.rq_metadata?.mockId`). - Skip logging when there is no `mockId`; an unmatched request has no mock to attach the log to (and the downstream sink keys logs by mockId). - Wrap the callback in try/catch so a logging failure can never escape the finish handler. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Warning Review limit reached
Next review available in: 40 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Walkthrough
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/middlewares/har.ts`:
- Line 35: Update the finish callback containing storageService.storeLog to
handle the returned Promise rejection: make the callback async and await
storeLog, or attach an equivalent catch handler so failures are processed by the
existing error path instead of becoming unhandled rejections.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: a3dd4eb5-751c-44c8-8f8e-63cf7dbde116
📒 Files selected for processing (1)
src/middlewares/har.ts
storageService.storeLog returns a Promise, so the synchronous try/catch did not cover a rejection from the sink's sendLog — it would surface as an unhandled rejection. Make the finish callback async and await the call so the rejection lands in the existing catch. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Problem
The HAR logging middleware registers a
res.once('finish', …)handler that readsres.locals.rq_metadata.mockId.rq_metadatais only populated for requests that resolve to a mock — for any unmatched request (e.g. a 404), it isundefined, and the access throwsTypeError: Cannot read properties of undefined (reading 'mockId').Because the throw happens inside the
finishevent callback (after the response is sent, outside the Express request/error pipeline), it isn't caught as a normal request error. It also means unmatched requests fail before any logging runs.Fix
mockId— an unmatched request has no mock to associate a log with.No behavior change for matched requests: they still log exactly as before.
Testing
npm run build(tsc) passes.finishhandler for both paths:rq_metadata) → no throw, logging skipped;mockIdand a populated HAR entry.🤖 Generated with Claude Code
Summary by CodeRabbit